//
//  GongLueViewController.m
//  MyProject
//
//  Created by apple on 15/11/2.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "GongLueViewController.h"
#import "AFNetworking.h"
#import "JSONModel.h"
#import "GongLueModel.h"
#import "GongLueDetailViewController.h"
#import "UIImageView+AFNetworking.h"
#import "LZXHelper.h"

#define URL @"http://chanyouji.com/api/wiki/destinations/%@.json"
@interface GongLueViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * _dataArr;
    UIActivityIndicatorView *_activity;
    UILabel * _lable;
    
}
@property (nonatomic,strong)UITableView *tabelView;

@end

@implementation GongLueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = [UIColor blackColor];
   
    self.title = self.name;
    
    [self getData];
    
    [self createView];
    [self createNavigationBar:9];
    self.back.text = @"风景";
    
}

-(void)getData
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    //NSLog(@"====%@",[NSString stringWithFormat:URL,self.ID]);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:URL,self.ID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary * dic in arr) {
            NSArray * pagesArr = dic[@"pages"];
            JSONModelArray * jsonArr = [[JSONModelArray alloc]initWithArray:pagesArr modelClass:[GongLueModel class]];
            [_dataArr addObject:jsonArr];
        }
        [_dataArr removeLastObject];
        
        [_tabelView reloadData];
        //下载完成之后 把数据 保存本地沙盒中(缓存)
        //直接把下载的二进制数据  responseObject写入本地文件
        NSString *path = [LZXHelper getFullPathWithFile:[NSString stringWithFormat:@"%@%@",URL,self.ID]];
        //写文件
        NSData *data = (NSData *)responseObject;
        //每一页 都做 缓存
        [data writeToFile:path atomically:YES];
        
        [_activity removeFromSuperview];
        [_lable removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)createView
{
        
    self.tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:1];
    [self.view addSubview:self.tabelView];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
   
    _lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
    _lable.center = self.view.center;
    [self.view addSubview:_lable];
    _lable.backgroundColor = [UIColor colorWithHue:50/255.f saturation:50/255.f brightness:50/255.f alpha:0.6];
    
    //活动指示器
    _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [self.view addSubview:_activity];
    _activity.center = self.view.center;
    [_activity startAnimating];
    
}

#pragma mark -代理方法 － 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataArr objectAtIndex:section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSMutableArray * arr = [_dataArr objectAtIndex:indexPath.section];
    GongLueModel * model = [arr objectAtIndex:indexPath.row];
    
    
    static NSString * cellId = @"111";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    cell.textLabel.text = model.title;
    cell.accessoryType = 1;
    
    
    return cell;
}

//点击了某个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableArray * arr = [_dataArr objectAtIndex:indexPath.section];
    
    GongLueModel * model = [arr objectAtIndex:indexPath.row];
    NSArray * childrenArr = model.children;
//    NSArray * childArr = [arr objectAtIndex:indexPath.row][@"children"];
//    
    GongLueDetailViewController * dongDetail = [[GongLueDetailViewController alloc]init];
    dongDetail.childrenArr = childrenArr;
    
    [self.navigationController pushViewController:dongDetail animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * arr = [_dataArr objectAtIndex:indexPath.section];
    GongLueModel * model = [arr objectAtIndex:indexPath.row];
    if (model.title.length == 0) {
        return 0;
    }
    return 44;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
